﻿


using HBase;
using hCommand;
using Net.http;
using Persistence;
using System;
using System.Collections.Generic;
using System.Linq;

namespace WebApi {
    [FormAuthenticationFilter]

    [RoutePrefix("doi")]
    public class DoiController : BaseApiController {
        IDoiServer doiServer = WMSDalFactray.getDal<IDoiServer>();
        ICoreStockServer coreStockServer = WMSDalFactray.getDal<ICoreStockServer>();
        [HttpPost, Route("FindPage")]
        public BaseResult FindPage([FromBody] DoDto dto) {
            Page<WhDoi> info = doiServer.QueryReceiptInPage(dto);

            return BaseResult.Ok(info);
        }



        [HttpPost, Route("batchDelete"), ControlName("入库单删除")]
        public BaseResult batchDelete([FromBody] List<WhDoi> list) {
            foreach (WhDoi receiptOut in list) {
                WhDoi model = doiServer.getById<WhDoi>(receiptOut.id);
                if (model.status != 1) {
                    return BaseResult.Error("入库单非新建待执行状态无法删除!");
                }

                doiServer.DeleteDetailByReceptId(receiptOut.id);
                doiServer.delete(model);

            }
            return BaseResult.Ok("ok");
        }


        [HttpPost, Route("getDetials")]
        public BaseResult getDetials([FromBody] WhDoi dto) {
            List<WhDoiDet> info = doiServer.getDetials(dto.id);
            return BaseResult.Ok(info);
        }

        [HttpPost, Route("finsh")]
        public BaseResult finsh([FromBody] WhDoi dto) {
            doiServer.finshDo(dto);
            return BaseResult.Ok("成功");
        }

        [HttpPost, Route("saveDoo"), ControlName("保存出库单及明细")]
        public BaseResult saveDoi([FromBody] DoiDto dto) {
            WhDoi doi = dto.main;
            if (doi.id == 0) {
                doi.status = 1;
                if (String.IsNullOrWhiteSpace(doi.batchNo)) {
                    doi.batchNo = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
                }
            } else {
                WhDoi receiptOut = doiServer.getById<WhDoi>(doi.id);
                if (receiptOut.status != 1) {
                    return BaseResult.Error("入库单非新建待执行状态无法修改!");
                }
            }
            doiServer.saveOne2Many(dto);
            return BaseResult.Ok(dto);
        }








        //入库明细
        [HttpPost, Route("GetReceiptInList")]
        public BaseResult GetReceiptInList([FromBody] DoDto dto) {
            var inReceiptDetail = doiServer.GetAll<WhDoiDet>(" AND id=@outDetailId", new { outDetailId = dto.outDetailId }).FirstOrDefault();
            if (inReceiptDetail == null) {
                return BaseResult.Error("没有对应绑定的出库单明细");
            }
            var outReceipt = doiServer.GetAll<WhDoo>(" AND id=@outId", new { outId = inReceiptDetail.pid }).FirstOrDefault();
            return BaseResult.Ok(outReceipt);
        }

        [HttpPost, Route("GetWhDoiDetList")]
        public BaseResult GetWhDoiDetList([FromBody] DoDto dto) {
            List<WhDoExcel> info = doiServer.GetExportList(dto);

            return BaseResult.Ok(info);
        }

    }
}